home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume90 / unix / uucp103d / part12 < prev    next >
Encoding:
Internet Message Format  |  1990-02-04  |  43.8 KB

  1. Path: xanth!cs.odu.edu!Amiga-Request
  2. From: Amiga-Request@cs.odu.edu (Amiga Sources/Binaries Moderator)
  3. Newsgroups: comp.sources.amiga
  4. Subject: v90i056: uucp 1.03D - unix compatible uucp/mail/news system, Part12/16
  5. Message-ID: <11295@xanth.cs.odu.edu>
  6. Date: 4 Feb 90 02:40:39 GMT
  7. Sender: tadguy@cs.odu.edu
  8. Reply-To: overload!dillon (Matt Dillon)
  9. Lines: 1620
  10. Approved: tadguy@cs.odu.edu (Tad Guy)
  11. X-Mail-Submissions-To: Amiga@cs.odu.edu
  12.  
  13. Submitted-by: overload!dillon (Matt Dillon)
  14. Posting-number: Volume 90, Issue 056
  15. Archive-name: unix/uucp-1.03d/part12
  16.  
  17. #!/bin/sh
  18. # This is a shell archive.  Remove anything before this line, then unpack
  19. # it by saving it into a file and typing "sh file".  To overwrite existing
  20. # files, type "sh file -c".  You can also feed this as standard input via
  21. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  22. # will see the following message at the end:
  23. #        "End of archive 12 (of 16)."
  24. # Contents:  src/compress/compress.c
  25. # Wrapped by tadguy@xanth on Sat Feb  3 20:51:20 1990
  26. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  27. if test -f 'src/compress/compress.c' -a "${1}" != "-c" ; then 
  28.   echo shar: Will not clobber existing file \"'src/compress/compress.c'\"
  29. else
  30. echo shar: Extracting \"'src/compress/compress.c'\" \(40885 characters\)
  31. sed "s/^X//" >'src/compress/compress.c' <<'END_OF_FILE'
  32. X
  33. X/*
  34. X * Compress - data compression program
  35. X */
  36. X
  37. X#include "/version.h"
  38. X
  39. XIDENT(".00");
  40. X
  41. X/*
  42. X * machine variants which require cc -Dmachine:  pdp11, z8000, pcxt
  43. X */
  44. X
  45. Xvoid Usage();
  46. Xvoid output();
  47. Xvoid writeerr();
  48. Xvoid copystat();
  49. Xvoid onintr();
  50. Xvoid cl_block();
  51. Xvoid cl_hash();
  52. Xvoid prratio();
  53. Xvoid version();
  54. Xvoid oops();
  55. X
  56. X
  57. X/*
  58. X * Set USERMEM to the maximum amount of physical user memory available
  59. X * in bytes.  USERMEM is used to determine the maximum BITS that can be used
  60. X * for compression.
  61. X *
  62. X * SACREDMEM is the amount of physical memory saved for others; compress
  63. X * will hog the rest.
  64. X */
  65. X
  66. X#ifndef SACREDMEM
  67. X#define SACREDMEM    0
  68. X#endif
  69. X
  70. X#ifndef USERMEM
  71. X/*# ifdef AMIGA
  72. X#   define USERMEM    200000
  73. X# else*/
  74. X#   define USERMEM    450000    /* default user memory */
  75. X/* # endif*/
  76. X#endif
  77. X
  78. X#ifdef interdata        /* (Perkin-Elmer) */
  79. X#define SIGNED_COMPARE_SLOW    /* signed compare is slower than unsigned */
  80. X#endif
  81. X
  82. X#ifdef pdp11
  83. X# define BITS    12    /* max bits/code for 16-bit machine */
  84. X# define NO_UCHAR    /* also if "unsigned char" functions as signed char */
  85. X# undef USERMEM
  86. X#endif /* pdp11 */    /* don't forget to compile with -i */
  87. X
  88. X#ifdef z8000
  89. X# define BITS    12
  90. X# undef vax        /* weird preprocessor */
  91. X# undef USERMEM
  92. X#endif /* z8000 */
  93. X
  94. X#ifdef pcxt
  95. X# define BITS    12
  96. X# undef USERMEM
  97. X#endif /* pcxt */
  98. X
  99. X#ifdef USERMEM
  100. X# if USERMEM >= (433484+SACREDMEM)
  101. X#  define PBITS 16
  102. X# else
  103. X#  if USERMEM >= (229600+SACREDMEM)
  104. X#   define PBITS    15
  105. X#  else
  106. X#   if USERMEM >= (127536+SACREDMEM)
  107. X#    define PBITS    14
  108. X#   else
  109. X#    if USERMEM >= (73464+SACREDMEM)
  110. X#     define PBITS    13
  111. X#    else
  112. X#     define PBITS    12
  113. X#    endif
  114. X#   endif
  115. X#  endif
  116. X# endif
  117. X# undef USERMEM
  118. X#endif /* USERMEM */
  119. X
  120. X#ifdef PBITS        /* Preferred BITS for this memory size */
  121. X# ifndef BITS
  122. X#  define BITS PBITS
  123. X# endif BITS
  124. X#endif /* PBITS */
  125. X
  126. X#if BITS == 16
  127. X# define HSIZE    69001        /* 95% occupancy */
  128. X#endif
  129. X#if BITS == 15
  130. X# define HSIZE    35023        /* 94% occupancy */
  131. X#endif
  132. X#if BITS == 14
  133. X# define HSIZE    18013        /* 91% occupancy */
  134. X#endif
  135. X#if BITS == 13
  136. X# define HSIZE    9001        /* 91% occupancy */
  137. X#endif
  138. X#if BITS <= 12
  139. X# define HSIZE    5003        /* 80% occupancy */
  140. X#endif
  141. X
  142. X#ifdef M_XENIX            /* Stupid compiler can't handle arrays with */
  143. X# if BITS == 16         /* more than 65535 bytes - so we fake it */
  144. X#  define XENIX_16
  145. X# else
  146. X#  if BITS > 13         /* Code only handles BITS = 12, 13, or 16 */
  147. X#   define BITS 13
  148. X#  endif
  149. X# endif
  150. X#endif
  151. X
  152. X/*
  153. X * a code_int must be able to hold 2**BITS values of type int, and also -1
  154. X */
  155. X#if BITS > 15
  156. Xtypedef long int    code_int;
  157. X#else
  158. Xtypedef int        code_int;
  159. X#endif
  160. X
  161. X#ifdef SIGNED_COMPARE_SLOW
  162. Xtypedef unsigned long int count_int;
  163. Xtypedef unsigned short int count_short;
  164. X#else
  165. Xtypedef long int      count_int;
  166. X#endif
  167. X
  168. X#ifdef NO_UCHAR
  169. X typedef char    char_type;
  170. X#else
  171. X typedef    unsigned char    char_type;
  172. X#endif /* UCHAR */
  173. Xchar_type magic_header[] = { "\037\235" };      /* 1F 9D */
  174. X
  175. X/* Defines for third byte of header */
  176. X#define BIT_MASK    0x1f
  177. X#define BLOCK_MASK    0x80
  178. X/* Masks 0x40 and 0x20 are free.  I think 0x20 should mean that there is
  179. X   a fourth header byte (for expansion).
  180. X*/
  181. X#define INIT_BITS 9            /* initial number of bits/code */
  182. X
  183. X/*
  184. X * compress.c - File compression ala IEEE Computer, June 1984.
  185. X *
  186. X * Authors:    Spencer W. Thomas    (decvax!harpo!utah-cs!utah-gr!thomas)
  187. X *        Jim McKie        (decvax!mcvax!jim)
  188. X *        Steve Davies        (decvax!vax135!petsd!peora!srd)
  189. X *        Ken Turkowski        (decvax!decwrl!turtlevax!ken)
  190. X *        James A. Woods        (decvax!ihnp4!ames!jaw)
  191. X *        Joe Orost        (decvax!vax135!petsd!joe)
  192. X *
  193. X * $Header: compress.c,v 4.0 85/07/30 12:50:00 joe Release $
  194. X * $Log:    compress.c,v $
  195. X * Revision 4.0  85/07/30  12:50:00  joe
  196. X * Removed ferror() calls in output routine on every output except first.
  197. X * Prepared for release to the world.
  198. X *
  199. X * Revision 3.6  85/07/04  01:22:21  joe
  200. X * Remove much wasted storage by overlaying hash table with the tables
  201. X * used by decompress: tab_suffix[1<<BITS], stack[8000].  Updated USERMEM
  202. X * computations.  Fixed dump_tab() DEBUG routine.
  203. X *
  204. X * Revision 3.5  85/06/30  20:47:21  jaw
  205. X * Change hash function to use exclusive-or.  Rip out hash cache.  These
  206. X * speedups render the megamemory version defunct, for now.  Make decoder
  207. X * stack global.  Parts of the RCS trunks 2.7, 2.6, and 2.1 no longer apply.
  208. X *
  209. X * Revision 3.4  85/06/27  12:00:00  ken
  210. X * Get rid of all floating-point calculations by doing all compression ratio
  211. X * calculations in fixed point.
  212. X *
  213. X * Revision 3.3  85/06/24  21:53:24  joe
  214. X * Incorporate portability suggestion for M_XENIX.  Got rid of text on #else
  215. X * and #endif lines.  Cleaned up #ifdefs for vax and interdata.
  216. X *
  217. X * Revision 3.2  85/06/06  21:53:24  jaw
  218. X * Incorporate portability suggestions for Z8000, IBM PC/XT from mailing list.
  219. X * Default to "quiet" output (no compression statistics).
  220. X *
  221. X * Revision 3.1  85/05/12  18:56:13  jaw
  222. X * Integrate decompress() stack speedups (from early pointer mods by McKie).
  223. X * Repair multi-file USERMEM gaffe.  Unify 'force' flags to mimic semantics
  224. X * of SVR2 'pack'.  Streamline block-compress table clear logic.  Increase
  225. X * output byte count by magic number size.
  226. X *
  227. X * Revision 3.0   84/11/27  11:50:00  petsd!joe
  228. X * Set HSIZE depending on BITS.  Set BITS depending on USERMEM.  Unrolled
  229. X * loops in clear routines.  Added "-C" flag for 2.0 compatibility.  Used
  230. X * unsigned compares on Perkin-Elmer.  Fixed foreground check.
  231. X *
  232. X * Revision 2.7   84/11/16  19:35:39  ames!jaw
  233. X * Cache common hash codes based on input statistics; this improves
  234. X * performance for low-density raster images.  Pass on #ifdef bundle
  235. X * from Turkowski.
  236. X *
  237. X * Revision 2.6   84/11/05  19:18:21  ames!jaw
  238. X * Vary size of hash tables to reduce time for small files.
  239. X * Tune PDP-11 hash function.
  240. X *
  241. X * Revision 2.5   84/10/30  20:15:14  ames!jaw
  242. X * Junk chaining; replace with the simpler (and, on the VAX, faster)
  243. X * double hashing, discussed within.  Make block compression standard.
  244. X *
  245. X * Revision 2.4   84/10/16  11:11:11  ames!jaw
  246. X * Introduce adaptive reset for block compression, to boost the rate
  247. X * another several percent.  (See mailing list notes.)
  248. X *
  249. X * Revision 2.3   84/09/22  22:00:00  petsd!joe
  250. X * Implemented "-B" block compress.  Implemented REVERSE sorting of tab_next.
  251. X * Bug fix for last bits.  Changed fwrite to putchar loop everywhere.
  252. X *
  253. X * Revision 2.2   84/09/18  14:12:21  ames!jaw
  254. X * Fold in news changes, small machine typedef from thomas,
  255. X * #ifdef interdata from joe.
  256. X *
  257. X * Revision 2.1   84/09/10  12:34:56  ames!jaw
  258. X * Configured fast table lookup for 32-bit machines.
  259. X * This cuts user time in half for b <= FBITS, and is useful for news batching
  260. X * from VAX to PDP sites.  Also sped up decompress() [fwrite->putc] and
  261. X * added signal catcher [plus beef in writeerr()] to delete effluvia.
  262. X *
  263. X * Revision 2.0   84/08/28  22:00:00  petsd!joe
  264. X * Add check for foreground before prompting user.  Insert maxbits into
  265. X * compressed file.  Force file being uncompressed to end with ".Z".
  266. X * Added "-c" flag and "zcat".  Prepared for release.
  267. X *
  268. X * Revision 1.10  84/08/24  18:28:00  turtlevax!ken
  269. X * Will only compress regular files (no directories), added a magic number
  270. X * header (plus an undocumented -n flag to handle old files without headers),
  271. X * added -f flag to force overwriting of possibly existing destination file,
  272. X * otherwise the user is prompted for a response.  Will tack on a .Z to a
  273. X * filename if it doesn't have one when decompressing.  Will only replace
  274. X * file if it was compressed.
  275. X *
  276. X * Revision 1.9  84/08/16  17:28:00  turtlevax!ken
  277. X * Removed scanargs(), getopt(), added .Z extension and unlimited number of
  278. X * filenames to compress.  Flags may be clustered (-Ddvb12) or separated
  279. X * (-D -d -v -b 12), or combination thereof.  Modes and other status is
  280. X * copied with copystat().  -O bug for 4.2 seems to have disappeared with
  281. X * 1.8.
  282. X *
  283. X * Revision 1.8  84/08/09  23:15:00  joe
  284. X * Made it compatible with vax version, installed jim's fixes/enhancements
  285. X *
  286. X * Revision 1.6  84/08/01  22:08:00  joe
  287. X * Sped up algorithm significantly by sorting the compress chain.
  288. X *
  289. X * Revision 1.5  84/07/13  13:11:00  srd
  290. X * Added C version of vax asm routines.  Changed structure to arrays to
  291. X * save much memory.  Do unsigned compares where possible (faster on
  292. X * Perkin-Elmer)
  293. X *
  294. X * Revision 1.4  84/07/05  03:11:11  thomas
  295. X * Clean up the code a little and lint it.  (Lint complains about all
  296. X * the regs used in the asm, but I'm not going to "fix" this.)
  297. X *
  298. X * Revision 1.3  84/07/05  02:06:54  thomas
  299. X * Minor fixes.
  300. X *
  301. X * Revision 1.2  84/07/05  00:27:27  thomas
  302. X * Add variable bit length output.
  303. X *
  304. X */
  305. X
  306. Xstatic char rcs_ident[] = "$Header: compress.c,v 4.0 85/07/30 12:50:00 joe Release $";
  307. X
  308. X#include <stdio.h>
  309. X#include <ctype.h>
  310. X#ifdef unix
  311. X#include <signal.h>
  312. X#include <sys/types.h>
  313. X#include <sys/stat.h>
  314. X#endif
  315. X
  316. X#ifndef min
  317. X#define min(a,b)        ((a>b) ? b : a)
  318. X#endif
  319. X
  320. X#define ARGVAL() (*++(*argv) || (--argc && *++argv))
  321. X
  322. Xint n_bits;                /* number of bits/code */
  323. Xint maxbits = BITS;            /* user settable max # bits/code */
  324. Xcode_int maxcode;            /* maximum code, given n_bits */
  325. Xcode_int maxmaxcode = 1 << BITS;    /* should NEVER generate this code */
  326. X#ifdef COMPATIBLE        /* But wrong! */
  327. X# define MAXCODE(n_bits)        (1 << (n_bits) - 1)
  328. X#else
  329. X# define MAXCODE(n_bits)        ((1 << (n_bits)) - 1)
  330. X#endif /* COMPATIBLE */
  331. X
  332. X#ifdef XENIX_16
  333. Xcount_int htab0[8192];
  334. Xcount_int htab1[8192];
  335. Xcount_int htab2[8192];
  336. Xcount_int htab3[8192];
  337. Xcount_int htab4[8192];
  338. Xcount_int htab5[8192];
  339. Xcount_int htab6[8192];
  340. Xcount_int htab7[8192];
  341. Xcount_int htab8[HSIZE-65536];
  342. Xcount_int * htab[9] = {
  343. X    htab0, htab1, htab2, htab3, htab4, htab5, htab6, htab7, htab8 };
  344. X
  345. X#define htabof(i)       (htab[(i) >> 13][(i) & 0x1fff])
  346. Xunsigned short code0tab[16384];
  347. Xunsigned short code1tab[16384];
  348. Xunsigned short code2tab[16384];
  349. Xunsigned short code3tab[16384];
  350. Xunsigned short code4tab[16384];
  351. Xunsigned short * codetab[5] = {
  352. X    code0tab, code1tab, code2tab, code3tab, code4tab };
  353. X
  354. X#define codetabof(i)    (codetab[(i) >> 14][(i) & 0x3fff])
  355. X
  356. X#else    /* Normal machine */
  357. Xcount_int htab [HSIZE];
  358. Xunsigned short codetab [HSIZE];
  359. X#define htabof(i)       htab[i]
  360. X#define codetabof(i)    codetab[i]
  361. X#endif    /* XENIX_16 */
  362. Xcode_int hsize = HSIZE;         /* for dynamic table sizing */
  363. Xcount_int fsize;
  364. X
  365. X/*
  366. X * To save much memory, we overlay the table used by compress() with those
  367. X * used by decompress().  The tab_prefix table is the same size and type
  368. X * as the codetab.  The tab_suffix table needs 2**BITS characters.  We
  369. X * get this from the beginning of htab.  The output stack uses the rest
  370. X * of htab, and contains characters.  There is plenty of room for any
  371. X * possible stack (stack used to be 8000 characters).
  372. X */
  373. X
  374. X#define tab_prefixof(i) codetabof(i)
  375. X#ifdef XENIX_16
  376. X# define tab_suffixof(i)        ((char_type *)htab[(i)>>15])[(i) & 0x7fff]
  377. X# define de_stack        ((char_type *)(htab2))
  378. X#else    /* Normal machine */
  379. X# define tab_suffixof(i)        ((char_type *)(htab))[i]
  380. X# define de_stack        ((char_type *)&tab_suffixof(1<<BITS))
  381. X#endif    /* XENIX_16 */
  382. X
  383. Xcode_int free_ent = 0;            /* first unused entry */
  384. Xint exit_stat = 0;
  385. X
  386. Xcode_int getcode();
  387. X
  388. Xvoid
  389. XUsage() {
  390. X#ifdef DEBUG
  391. Xfprintf(stderr,"Usage: compress [-dDVfc] [-b maxbits] [file ...]\n");
  392. X}
  393. Xint debug = 0;
  394. X#else
  395. Xfprintf(stderr,"Usage: compress [-dfvcV] [-b maxbits] [file ...]\n");
  396. X}
  397. X#endif /* DEBUG */
  398. Xint nomagic = 0;    /* Use a 3-byte magic number header, unless old file */
  399. Xint zcat_flg = 0;    /* Write output on stdout, suppress messages */
  400. Xint quiet = 1;        /* don't tell me about compression */
  401. X
  402. X/*
  403. X * block compression parameters -- after all codes are used up,
  404. X * and compression rate changes, start over.
  405. X */
  406. Xint block_compress = BLOCK_MASK;
  407. Xint clear_flg = 0;
  408. Xlong int ratio = 0;
  409. X#define CHECK_GAP 10000 /* ratio check interval */
  410. Xcount_int checkpoint = CHECK_GAP;
  411. X/*
  412. X * the next two codes should not be changed lightly, as they must not
  413. X * lie within the contiguous general code space.
  414. X */
  415. X#define FIRST    257    /* first free entry */
  416. X#define CLEAR    256    /* table clear output code */
  417. X
  418. Xint force = 0;
  419. Xchar ofname [100];
  420. X#ifdef DEBUG
  421. Xint verbose = 0;
  422. X#endif /* DEBUG */
  423. Xint (*bgnd_flag)();
  424. X
  425. Xint do_decomp = 0;
  426. X
  427. Xstatic void decompress ();
  428. Xstatic void compress ();
  429. X
  430. X/*****************************************************************
  431. X * TAG( main )
  432. X *
  433. X * Algorithm from "A Technique for High Performance Data Compression",
  434. X * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.
  435. X *
  436. X * Usage: compress [-dfvc] [-b bits] [file ...]
  437. X * Inputs:
  438. X *    -d:        If given, decompression is done instead.
  439. X *
  440. X *    -c:        Write output on stdout, don't remove original.
  441. X *
  442. X *    -b:        Parameter limits the max number of bits/code.
  443. X *
  444. X *    -f:        Forces output file to be generated, even if one already
  445. X *            exists, and even if no space is saved by compressing.
  446. X *            If -f is not used, the user will be prompted if stdin is
  447. X *            a tty, otherwise, the output file will not be overwritten.
  448. X *
  449. X *    -v:        Write compression statistics
  450. X *
  451. X *    file ...:   Files to be compressed.  If none specified, stdin
  452. X *            is used.
  453. X * Outputs:
  454. X *    file.Z:     Compressed form of file with same mode, owner, and utimes
  455. X *    or stdout   (if stdin used as input)
  456. X *
  457. X * Assumptions:
  458. X *    When filenames are given, replaces with the compressed version
  459. X *    (.Z suffix) only if the file decreases in size.
  460. X * Algorithm:
  461. X *    Modified Lempel-Ziv method (LZW).  Basically finds common
  462. X * substrings and replaces them with a variable size code.  This is
  463. X * deterministic, and can be done on the fly.  Thus, the decompression
  464. X * procedure needs no input table, but tracks the way the table was built.
  465. X */
  466. X
  467. X
  468. Xvoid
  469. Xmain( argc, argv )
  470. Xregister int argc; char **argv;
  471. X{
  472. X    int overwrite = 0;    /* Do not overwrite unless given -f flag */
  473. X    char tempname[100];
  474. X    char **filelist, **fileptr;
  475. X    char *cp, *rindex(), *malloc();
  476. X
  477. X#ifdef unix
  478. X    if ( (bgnd_flag = signal ( SIGINT, SIG_IGN )) != SIG_IGN ) {
  479. X    signal ( SIGINT, onintr );
  480. X    signal ( SIGSEGV, oops );
  481. X    }
  482. X#endif
  483. X
  484. X#ifdef COMPATIBLE
  485. X    nomagic = 1;    /* Original didn't have a magic number */
  486. X#endif /* COMPATIBLE */
  487. X
  488. X    filelist = fileptr = (char **)(malloc(argc * sizeof(*argv)));
  489. X    *filelist = NULL;
  490. X
  491. X    if((cp = rindex(argv[0], '/')) != 0) {
  492. X    cp++;
  493. X    } else {
  494. X    cp = argv[0];
  495. X    }
  496. X    if(strcmp(cp, "uncompress") == 0) {
  497. X    do_decomp = 1;
  498. X    } else if(strcmp(cp, "zcat") == 0) {
  499. X    do_decomp = 1;
  500. X    zcat_flg = 1;
  501. X    }
  502. X
  503. X#ifdef BSD4_2
  504. X    /* 4.2BSD dependent - take it out if not */
  505. X    setlinebuf( stderr );
  506. X#endif /* BSD4_2 */
  507. X
  508. X    /* Argument Processing
  509. X     * All flags are optional.
  510. X     * -D => debug
  511. X     * -V => print Version; debug verbose
  512. X     * -d => do_decomp
  513. X     * -v => unquiet
  514. X     * -f => force overwrite of output file
  515. X     * -n => no header: useful to uncompress old files
  516. X     * -b maxbits => maxbits.  If -b is specified, then maxbits MUST be
  517. X     *        given also.
  518. X     * -c => cat all output to stdout
  519. X     * -C => generate output compatible with compress 2.0.
  520. X     * if a string is left, must be an input filename.
  521. X     */
  522. X    for (argc--, argv++; argc > 0; argc--, argv++) {
  523. X    if (**argv == '-') {    /* A flag argument */
  524. X        while (*++(*argv)) {        /* Process all flags in this arg */
  525. X        switch (**argv) {
  526. X#ifdef DEBUG
  527. X            case 'D':
  528. X            debug = 1;
  529. X            break;
  530. X            case 'V':
  531. X            verbose = 1;
  532. X            version();
  533. X            break;
  534. X#else
  535. X            case 'V':
  536. X            version();
  537. X            break;
  538. X#endif /* DEBUG */
  539. X            case 'v':
  540. X            quiet = 0;
  541. X            break;
  542. X            case 'd':
  543. X            do_decomp = 1;
  544. X            break;
  545. X            case 'f':
  546. X            case 'F':
  547. X            overwrite = 1;
  548. X            force = 1;
  549. X            break;
  550. X            case 'n':
  551. X            nomagic = 1;
  552. X            break;
  553. X            case 'C':
  554. X            block_compress = 0;
  555. X            break;
  556. X            case 'b':
  557. X            if (!ARGVAL()) {
  558. X                fprintf(stderr, "Missing maxbits\n");
  559. X                Usage();
  560. X                exit(1);
  561. X            }
  562. X            maxbits = atoi(*argv);
  563. X            goto nextarg;
  564. X            case 'c':
  565. X            zcat_flg = 1;
  566. X            break;
  567. X            case 'q':
  568. X            quiet = 1;
  569. X            break;
  570. X            default:
  571. X            fprintf(stderr, "Unknown flag: '%c'; ", **argv);
  572. X            Usage();
  573. X            exit(1);
  574. X        }
  575. X        }
  576. X    }
  577. X    else {        /* Input file name */
  578. X        *fileptr++ = *argv; /* Build input file list */
  579. X        *fileptr = NULL;
  580. X        /* process nextarg; */
  581. X    }
  582. X    nextarg: continue;
  583. X    }
  584. X
  585. X    if(maxbits < INIT_BITS) maxbits = INIT_BITS;
  586. X    if (maxbits > BITS) maxbits = BITS;
  587. X    maxmaxcode = 1 << maxbits;
  588. X
  589. X    if (*filelist != NULL) {
  590. X    for (fileptr = filelist; *fileptr; fileptr++) {
  591. X        exit_stat = 0;
  592. X        if (do_decomp != 0) {                       /* DECOMPRESSION */
  593. X        /* Check for .Z suffix */
  594. X        if (strcmp(*fileptr + strlen(*fileptr) - 2, ".Z") != 0) {
  595. X            /* No .Z: tack one on */
  596. X            strcpy(tempname, *fileptr);
  597. X            strcat(tempname, ".Z");
  598. X            *fileptr = tempname;
  599. X        }
  600. X        /* Open input file */
  601. X        if ((freopen(*fileptr, "r", stdin)) == NULL) {
  602. X            perror(*fileptr); continue;
  603. X        }
  604. X        /* Check the magic number */
  605. X        if (nomagic == 0) {
  606. X            if ((getchar() != (magic_header[0] & 0xFF))
  607. X             || (getchar() != (magic_header[1] & 0xFF))) {
  608. X            fprintf(stderr, "%s: not in compressed format\n",
  609. X                *fileptr);
  610. X            continue;
  611. X            }
  612. X            maxbits = getchar();        /* set -b from file */
  613. X            block_compress = maxbits & BLOCK_MASK;
  614. X            maxbits &= BIT_MASK;
  615. X            maxmaxcode = 1 << maxbits;
  616. X            if(maxbits > BITS) {
  617. X            fprintf(stderr,
  618. X            "%s: compressed with %d bits, can only handle %d bits\n",
  619. X            *fileptr, maxbits, BITS);
  620. X            continue;
  621. X            }
  622. X        }
  623. X        /* Generate output filename */
  624. X        strcpy(ofname, *fileptr);
  625. X        ofname[strlen(*fileptr) - 2] = '\0';  /* Strip off .Z */
  626. X        } else {                    /* COMPRESSION */
  627. X        if (strcmp(*fileptr + strlen(*fileptr) - 2, ".Z") == 0) {
  628. X            fprintf(stderr, "%s: already has .Z suffix -- no change\n",
  629. X                *fileptr);
  630. X            continue;
  631. X        }
  632. X        /* Open input file */
  633. X        if ((freopen(*fileptr, "r", stdin)) == NULL) {
  634. X            perror(*fileptr); continue;
  635. X        }
  636. X        fsize = getfilesize (*fileptr);
  637. X        /*
  638. X         * tune hash table size for small files -- ad hoc,
  639. X         * but the sizes match earlier #defines, which
  640. X         * serve as upper bounds on the number of output codes.
  641. X         */
  642. X        hsize = HSIZE;
  643. X        if ( fsize < (1 << 12) )
  644. X            hsize = min ( 5003, HSIZE );
  645. X        else if ( fsize < (1 << 13) )
  646. X            hsize = min ( 9001, HSIZE );
  647. X        else if ( fsize < (1 << 14) )
  648. X            hsize = min ( 18013, HSIZE );
  649. X        else if ( fsize < (1 << 15) )
  650. X            hsize = min ( 35023, HSIZE );
  651. X        else if ( fsize < 47000 )
  652. X            hsize = min ( 50021, HSIZE );
  653. X
  654. X        /* Generate output filename */
  655. X        strcpy(ofname, *fileptr);
  656. X#ifndef BSD4_2        /* Short filenames */
  657. X        if ((cp=rindex(ofname,'/')) != NULL)    cp++;
  658. X        else                    cp = ofname;
  659. X        if (strlen(cp) > 12) {
  660. X            fprintf(stderr,"%s: filename too long to tack on .Z\n",cp);
  661. X            continue;
  662. X        }
  663. X#endif    /* BSD4_2        Long filenames allowed */
  664. X        strcat(ofname, ".Z");
  665. X        }
  666. X        /* Check for overwrite of existing file */
  667. X#ifdef unix
  668. X        if (overwrite == 0 && zcat_flg == 0) {
  669. X        if (stat(ofname, &statbuf) == 0) {
  670. X            char response[2];
  671. X            response[0] = 'n';
  672. X            fprintf(stderr, "%s already exists;", ofname);
  673. X            if (foreground()) {
  674. X            fprintf(stderr, " do you wish to overwrite %s (y or n)? ",
  675. X            ofname);
  676. X            fflush(stderr);
  677. X            read(2, response, 2);
  678. X            while (response[1] != '\n') {
  679. X                if (read(2, response+1, 1) < 0) {   /* Ack! */
  680. X                perror("stderr"); break;
  681. X                }
  682. X            }
  683. X            }
  684. X            if (response[0] != 'y') {
  685. X            fprintf(stderr, "\tnot overwritten\n");
  686. X            continue;
  687. X            }
  688. X        }
  689. X        }
  690. X#endif
  691. X        if(zcat_flg == 0) {         /* Open output file */
  692. X        if (freopen(ofname, "w", stdout) == NULL) {
  693. X            perror(ofname);
  694. X            continue;
  695. X        }
  696. X        if(!quiet)
  697. X            fprintf(stderr, "%s: ", *fileptr);
  698. X        }
  699. X
  700. X        /* Actually do the compression/decompression */
  701. X        if (do_decomp == 0) compress();
  702. X#ifndef DEBUG
  703. X        else            decompress();
  704. X#else
  705. X        else if (debug == 0)        decompress();
  706. X        else            printcodes();
  707. X        if (verbose)                dump_tab();
  708. X#endif /* DEBUG */
  709. X        if(zcat_flg == 0) {
  710. X        copystat(*fileptr, ofname);     /* Copy stats */
  711. X        if((exit_stat == 1) || (!quiet))
  712. X            putc('\n', stderr);
  713. X        }
  714. X    }
  715. X    } else {        /* Standard input */
  716. X    if (do_decomp == 0) {
  717. X        compress();
  718. X#ifdef DEBUG
  719. X        if(verbose)             dump_tab();
  720. X#endif /* DEBUG */
  721. X        if(!quiet)
  722. X            putc('\n', stderr);
  723. X    } else {
  724. X        /* Check the magic number */
  725. X        if (nomagic == 0) {
  726. X        if ((getchar()!=(magic_header[0] & 0xFF))
  727. X         || (getchar()!=(magic_header[1] & 0xFF))) {
  728. X            fprintf(stderr, "stdin: not in compressed format\n");
  729. X            exit(1);
  730. X        }
  731. X        maxbits = getchar();    /* set -b from file */
  732. X        block_compress = maxbits & BLOCK_MASK;
  733. X        maxbits &= BIT_MASK;
  734. X        maxmaxcode = 1 << maxbits;
  735. X        fsize = 100000;     /* assume stdin large for USERMEM */
  736. X        if(maxbits > BITS) {
  737. X            fprintf(stderr,
  738. X            "stdin: compressed with %d bits, can only handle %d bits\n",
  739. X            maxbits, BITS);
  740. X            exit(1);
  741. X        }
  742. X        }
  743. X#ifndef DEBUG
  744. X        decompress();
  745. X#else
  746. X        if (debug == 0)     decompress();
  747. X        else        printcodes();
  748. X        if (verbose)        dump_tab();
  749. X#endif /* DEBUG */
  750. X    }
  751. X    }
  752. X    exit(exit_stat);
  753. X}
  754. X
  755. Xstatic int offset;
  756. Xlong int in_count = 1;            /* length of input */
  757. Xlong int bytes_out;            /* length of compressed output */
  758. Xlong int out_count = 0;         /* # of codes output (for debugging) */
  759. X
  760. X/*
  761. X * compress stdin to stdout
  762. X *
  763. X * Algorithm:  use open addressing double hashing (no chaining) on the
  764. X * prefix code / next character combination.  We do a variant of Knuth's
  765. X * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
  766. X * secondary probe.  Here, the modular division first probe is gives way
  767. X * to a faster exclusive-or manipulation.  Also do block compression with
  768. X * an adaptive reset, whereby the code table is cleared when the compression
  769. X * ratio decreases, but after the table fills.    The variable-length output
  770. X * codes are re-sized at this point, and a special CLEAR code is generated
  771. X * for the decompressor.  Late addition:  construct the table according to
  772. X * file size for noticeable speed improvement on small files.  Please direct
  773. X * questions about this implementation to ames!jaw.
  774. X */
  775. X
  776. Xstatic void compress() {
  777. X    register long fcode;
  778. X    register code_int i = 0;
  779. X    register int c;
  780. X    register code_int ent;
  781. X#ifdef XENIX_16
  782. X    register code_int disp;
  783. X#else    /* Normal machine */
  784. X    register int disp;
  785. X#endif
  786. X    register code_int hsize_reg;
  787. X    register int hshift;
  788. X
  789. X#ifndef COMPATIBLE
  790. X    if (nomagic == 0) {
  791. X    putchar(magic_header[0]); putchar(magic_header[1]);
  792. X    putchar((char)(maxbits | block_compress));
  793. X    if(ferror(stdout))
  794. X        writeerr();
  795. X    }
  796. X#endif /* COMPATIBLE */
  797. X
  798. X    offset = 0;
  799. X    bytes_out = 3;        /* includes 3-byte header mojo */
  800. X    out_count = 0;
  801. X    clear_flg = 0;
  802. X    ratio = 0;
  803. X    in_count = 1;
  804. X    checkpoint = CHECK_GAP;
  805. X    maxcode = MAXCODE(n_bits = INIT_BITS);
  806. X    free_ent = ((block_compress) ? FIRST : 256 );
  807. X
  808. X    ent = getchar ();
  809. X
  810. X    hshift = 0;
  811. X    for ( fcode = (long) hsize;  fcode < 65536L; fcode *= 2L )
  812. X    hshift++;
  813. X    hshift = 8 - hshift;        /* set hash code range bound */
  814. X
  815. X    hsize_reg = hsize;
  816. X    cl_hash( (count_int) hsize_reg);            /* clear hash table */
  817. X
  818. X#ifdef SIGNED_COMPARE_SLOW
  819. X    while ( (c = getchar()) != (unsigned) EOF ) {
  820. X#else
  821. X    while ( (c = getchar()) != EOF ) {
  822. X#endif
  823. X    in_count++;
  824. X    fcode = (long) (((long) c << maxbits) + ent);
  825. X    i = ((c << hshift) ^ ent);      /* xor hashing */
  826. X
  827. X    if ( htabof (i) == fcode ) {
  828. X        ent = codetabof (i);
  829. X        continue;
  830. X    } else if ( (long)htabof (i) < 0 )      /* empty slot */
  831. X        goto nomatch;
  832. X    disp = hsize_reg - i;        /* secondary hash (after G. Knott) */
  833. X    if ( i == 0 )
  834. X        disp = 1;
  835. Xprobe:
  836. X    if ( (i -= disp) < 0 )
  837. X        i += hsize_reg;
  838. X
  839. X    if ( htabof (i) == fcode ) {
  840. X        ent = codetabof (i);
  841. X        continue;
  842. X    }
  843. X    if ( (long)htabof (i) > 0 )
  844. X        goto probe;
  845. Xnomatch:
  846. X    output ( (code_int) ent );
  847. X    out_count++;
  848. X    ent = c;
  849. X#ifdef SIGNED_COMPARE_SLOW
  850. X    if ( (unsigned) free_ent < (unsigned) maxmaxcode) {
  851. X#else
  852. X    if ( free_ent < maxmaxcode ) {
  853. X#endif
  854. X        codetabof (i) = free_ent++; /* code -> hashtable */
  855. X        htabof (i) = fcode;
  856. X    }
  857. X    else if ( (count_int)in_count >= checkpoint && block_compress )
  858. X        cl_block ();
  859. X    }
  860. X    /*
  861. X     * Put out the final code.
  862. X     */
  863. X    output( (code_int)ent );
  864. X    out_count++;
  865. X    output( (code_int)-1 );
  866. X
  867. X    /*
  868. X     * Print out stats on stderr
  869. X     */
  870. X    if(zcat_flg == 0 && !quiet) {
  871. X#ifdef DEBUG
  872. X    fprintf( stderr,
  873. X        "%ld chars in, %ld codes (%ld bytes) out, compression factor: ",
  874. X        in_count, out_count, bytes_out );
  875. X    prratio( stderr, in_count, bytes_out );
  876. X    fprintf( stderr, "\n");
  877. X    fprintf( stderr, "\tCompression as in compact: " );
  878. X    prratio( stderr, in_count-bytes_out, in_count );
  879. X    fprintf( stderr, "\n");
  880. X    fprintf( stderr, "\tLargest code (of last block) was %d (%d bits)\n",
  881. X        free_ent - 1, n_bits );
  882. X#else /* !DEBUG */
  883. X    fprintf( stderr, "Compression: " );
  884. X    prratio( stderr, in_count-bytes_out, in_count );
  885. X#endif /* DEBUG */
  886. X    }
  887. X    if(bytes_out > in_count)    /* exit(2) if no savings */
  888. X    exit_stat = 2;
  889. X    return;
  890. X}
  891. X
  892. X/*****************************************************************
  893. X * TAG( output )
  894. X *
  895. X * Output the given code.
  896. X * Inputs:
  897. X *    code:    A n_bits-bit integer.  If == -1, then EOF.  This assumes
  898. X *        that n_bits =< (long)wordsize - 1.
  899. X * Outputs:
  900. X *    Outputs code to the file.
  901. X * Assumptions:
  902. X *    Chars are 8 bits long.
  903. X * Algorithm:
  904. X *    Maintain a BITS character long buffer (so that 8 codes will
  905. X * fit in it exactly).    Use the VAX insv instruction to insert each
  906. X * code in turn.  When the buffer fills up empty it and start over.
  907. X */
  908. X
  909. Xstatic char buf[BITS];
  910. X
  911. X#ifndef vax
  912. Xchar_type lmask[9] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
  913. Xchar_type rmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
  914. X#endif /* vax */
  915. X
  916. Xvoid
  917. Xoutput( code )
  918. Xcode_int  code;
  919. X{
  920. X#ifdef DEBUG
  921. X    static int col = 0;
  922. X#endif /* DEBUG */
  923. X
  924. X    /*
  925. X     * On the VAX, it is important to have the register declarations
  926. X     * in exactly the order given, or the asm will break.
  927. X     */
  928. X    register int r_off = offset, bits= n_bits;
  929. X    register char * bp = buf;
  930. X
  931. X#ifdef DEBUG
  932. X    if ( verbose )
  933. X        fprintf( stderr, "%5d%c", code,
  934. X            (col+=6) >= 74 ? (col = 0, '\n') : ' ' );
  935. X#endif /* DEBUG */
  936. X    if ( code >= 0 ) {
  937. X#ifdef vax
  938. X    /* VAX DEPENDENT!! Implementation on other machines is below.
  939. X     *
  940. X     * Translation: Insert BITS bits from the argument starting at
  941. X     * offset bits from the beginning of buf.
  942. X     */
  943. X    0;    /* Work around for pcc -O bug with asm and if stmt */
  944. X    asm( "insv      4(ap),r11,r10,(r9)" );
  945. X#else /* not a vax */
  946. X/*
  947. X * byte/bit numbering on the VAX is simulated by the following code
  948. X */
  949. X    /*
  950. X     * Get to the first byte.
  951. X     */
  952. X    bp += (r_off >> 3);
  953. X    r_off &= 7;
  954. X    /*
  955. X     * Since code is always >= 8 bits, only need to mask the first
  956. X     * hunk on the left.
  957. X     */
  958. X    *bp = (*bp & rmask[r_off]) | (code << r_off) & lmask[r_off];
  959. X    bp++;
  960. X    bits -= (8 - r_off);
  961. X    code >>= 8 - r_off;
  962. X    /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
  963. X    if ( bits >= 8 ) {
  964. X        *bp++ = code;
  965. X        code >>= 8;
  966. X        bits -= 8;
  967. X    }
  968. X    /* Last bits. */
  969. X    if(bits)
  970. X        *bp = code;
  971. X#endif /* vax */
  972. X    offset += n_bits;
  973. X    if ( offset == (n_bits << 3) ) {
  974. X        bp = buf;
  975. X        bits = n_bits;
  976. X        bytes_out += bits;
  977. X        do
  978. X        putchar(*bp++);
  979. X        while(--bits);
  980. X        offset = 0;
  981. X    }
  982. X
  983. X    /*
  984. X     * If the next entry is going to be too big for the code size,
  985. X     * then increase it, if possible.
  986. X     */
  987. X    if ( free_ent > maxcode || (clear_flg > 0))
  988. X    {
  989. X        /*
  990. X         * Write the whole buffer, because the input side won't
  991. X         * discover the size increase until after it has read it.
  992. X         */
  993. X        if ( offset > 0 ) {
  994. X        if( fwrite( buf, 1, n_bits, stdout ) != n_bits)
  995. X            writeerr();
  996. X        bytes_out += n_bits;
  997. X        }
  998. X        offset = 0;
  999. X
  1000. X        if ( clear_flg ) {
  1001. X        maxcode = MAXCODE (n_bits = INIT_BITS);
  1002. X        clear_flg = 0;
  1003. X        }
  1004. X        else {
  1005. X        n_bits++;
  1006. X        if ( n_bits == maxbits )
  1007. X            maxcode = maxmaxcode;
  1008. X        else
  1009. X            maxcode = MAXCODE(n_bits);
  1010. X        }
  1011. X#ifdef DEBUG
  1012. X        if ( debug ) {
  1013. X        fprintf( stderr, "\nChange to %d bits\n", n_bits );
  1014. X        col = 0;
  1015. X        }
  1016. X#endif /* DEBUG */
  1017. X    }
  1018. X    } else {
  1019. X    /*
  1020. X     * At EOF, write the rest of the buffer.
  1021. X     */
  1022. X    if ( offset > 0 )
  1023. X        fwrite( buf, 1, (offset + 7) / 8, stdout );
  1024. X    bytes_out += (offset + 7) / 8;
  1025. X    offset = 0;
  1026. X    fflush( stdout );
  1027. X#ifdef DEBUG
  1028. X    if ( verbose )
  1029. X        fprintf( stderr, "\n" );
  1030. X#endif /* DEBUG */
  1031. X    if( ferror( stdout ) )
  1032. X        writeerr();
  1033. X    }
  1034. X}
  1035. X
  1036. X/*
  1037. X * Decompress stdin to stdout.    This routine adapts to the codes in the
  1038. X * file building the "string" table on-the-fly; requiring no table to
  1039. X * be stored in the compressed file.  The tables used herein are shared
  1040. X * with those of the compress() routine.  See the definitions above.
  1041. X */
  1042. X
  1043. Xstatic void decompress() {
  1044. X    register char_type *stackp;
  1045. X    register int finchar;
  1046. X    register code_int code, oldcode, incode;
  1047. X
  1048. X    /*
  1049. X     * As above, initialize the first 256 entries in the table.
  1050. X     */
  1051. X    maxcode = MAXCODE(n_bits = INIT_BITS);
  1052. X    for ( code = 255; code >= 0; code-- ) {
  1053. X    tab_prefixof(code) = 0;
  1054. X    tab_suffixof(code) = (char_type)code;
  1055. X    }
  1056. X    free_ent = ((block_compress) ? FIRST : 256 );
  1057. X
  1058. X    finchar = oldcode = getcode();
  1059. X    if(oldcode == -1)   /* EOF already? */
  1060. X    return;         /* Get out of here */
  1061. X    putchar( (char)finchar );           /* first code must be 8 bits = char */
  1062. X    if(ferror(stdout))          /* Crash if can't write */
  1063. X    writeerr();
  1064. X    stackp = de_stack;
  1065. X
  1066. X    while ( (code = getcode()) > -1 ) {
  1067. X
  1068. X    if ( (code == CLEAR) && block_compress ) {
  1069. X        for ( code = 255; code >= 0; code-- )
  1070. X        tab_prefixof(code) = 0;
  1071. X        clear_flg = 1;
  1072. X        free_ent = FIRST - 1;
  1073. X        if ( (code = getcode ()) == -1 )    /* O, untimely death! */
  1074. X        break;
  1075. X    }
  1076. X    incode = code;
  1077. X    /*
  1078. X     * Special case for KwKwK string.
  1079. X     */
  1080. X    if ( code >= free_ent ) {
  1081. X        *stackp++ = finchar;
  1082. X        code = oldcode;
  1083. X    }
  1084. X
  1085. X    /*
  1086. X     * Generate output characters in reverse order
  1087. X     */
  1088. X#ifdef SIGNED_COMPARE_SLOW
  1089. X    while ( ((unsigned long)code) >= ((unsigned long)256) ) {
  1090. X#else
  1091. X    while ( code >= 256 ) {
  1092. X#endif
  1093. X        *stackp++ = tab_suffixof(code);
  1094. X        code = tab_prefixof(code);
  1095. X    }
  1096. X    *stackp++ = finchar = tab_suffixof(code);
  1097. X
  1098. X    /*
  1099. X     * And put them out in forward order
  1100. X     */
  1101. X    do
  1102. X        putchar ( *--stackp );
  1103. X    while ( stackp > de_stack );
  1104. X
  1105. X    /*
  1106. X     * Generate the new entry.
  1107. X     */
  1108. X    if ( (code=free_ent) < maxmaxcode ) {
  1109. X        tab_prefixof(code) = (unsigned short)oldcode;
  1110. X        tab_suffixof(code) = finchar;
  1111. X        free_ent = code+1;
  1112. X    }
  1113. X    /*
  1114. X     * Remember previous code.
  1115. X     */
  1116. X    oldcode = incode;
  1117. X    }
  1118. X    fflush( stdout );
  1119. X    if(ferror(stdout))
  1120. X    writeerr();
  1121. X}
  1122. X
  1123. X/*****************************************************************
  1124. X * TAG( getcode )
  1125. X *
  1126. X * Read one code from the standard input.  If EOF, return -1.
  1127. X * Inputs:
  1128. X *    stdin
  1129. X * Outputs:
  1130. X *    code or -1 is returned.
  1131. X */
  1132. X
  1133. Xcode_int
  1134. Xgetcode() {
  1135. X    /*
  1136. X     * On the VAX, it is important to have the register declarations
  1137. X     * in exactly the order given, or the asm will break.
  1138. X     */
  1139. X    register code_int code;
  1140. X    static int offset = 0, size = 0;
  1141. X    static char_type buf[BITS];
  1142. X    register int r_off, bits;
  1143. X    register char_type *bp = buf;
  1144. X
  1145. X    if ( clear_flg > 0 || offset >= size || free_ent > maxcode ) {
  1146. X    /*
  1147. X     * If the next entry will be too big for the current code
  1148. X     * size, then we must increase the size.  This implies reading
  1149. X     * a new buffer full, too.
  1150. X     */
  1151. X    if ( free_ent > maxcode ) {
  1152. X        n_bits++;
  1153. X        if ( n_bits == maxbits )
  1154. X        maxcode = maxmaxcode;    /* won't get any bigger now */
  1155. X        else
  1156. X        maxcode = MAXCODE(n_bits);
  1157. X    }
  1158. X    if ( clear_flg > 0) {
  1159. X        maxcode = MAXCODE (n_bits = INIT_BITS);
  1160. X        clear_flg = 0;
  1161. X    }
  1162. X    size = fread( buf, 1, n_bits, stdin );
  1163. X    if ( size <= 0 )
  1164. X        return -1;            /* end of file */
  1165. X    offset = 0;
  1166. X    /* Round size down to integral number of codes */
  1167. X    size = (size << 3) - (n_bits - 1);
  1168. X    }
  1169. X    r_off = offset;
  1170. X    bits = n_bits;
  1171. X#ifdef vax
  1172. X    asm( "extzv   r10,r9,(r8),r11" );
  1173. X#else /* not a vax */
  1174. X    /*
  1175. X     * Get to the first byte.
  1176. X     */
  1177. X    bp += (r_off >> 3);
  1178. X    r_off &= 7;
  1179. X    /* Get first part (low order bits) */
  1180. X#ifdef NO_UCHAR
  1181. X    code = ((*bp++ >> r_off) & rmask[8 - r_off]) & 0xff;
  1182. X#else
  1183. X    code = (*bp++ >> r_off);
  1184. X#endif /* NO_UCHAR */
  1185. X    bits -= (8 - r_off);
  1186. X    r_off = 8 - r_off;        /* now, offset into code word */
  1187. X    /* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
  1188. X    if ( bits >= 8 ) {
  1189. X#ifdef NO_UCHAR
  1190. X        code |= (*bp++ & 0xff) << r_off;
  1191. X#else
  1192. X        code |= *bp++ << r_off;
  1193. X#endif /* NO_UCHAR */
  1194. X        r_off += 8;
  1195. X        bits -= 8;
  1196. X    }
  1197. X    /* high order bits. */
  1198. X    code |= (*bp & rmask[bits]) << r_off;
  1199. X#endif /* vax */
  1200. X    offset += n_bits;
  1201. X
  1202. X    return code;
  1203. X}
  1204. X
  1205. Xchar *
  1206. Xrindex(s, c)            /* For those who don't have it in libc.a */
  1207. Xregister char *s, c;
  1208. X{
  1209. X    char *p;
  1210. X    for (p = NULL; *s; s++)
  1211. X        if (*s == c)
  1212. X        p = s;
  1213. X    return(p);
  1214. X}
  1215. X
  1216. X#ifdef DEBUG
  1217. Xprintcodes()
  1218. X{
  1219. X    /*
  1220. X     * Just print out codes from input file.  For debugging.
  1221. X     */
  1222. X    code_int code;
  1223. X    int col = 0, bits;
  1224. X
  1225. X    bits = n_bits = INIT_BITS;
  1226. X    maxcode = MAXCODE(n_bits);
  1227. X    free_ent = ((block_compress) ? FIRST : 256 );
  1228. X    while ( ( code = getcode() ) >= 0 ) {
  1229. X    if ( (code == CLEAR) && block_compress ) {
  1230. X        free_ent = FIRST - 1;
  1231. X        clear_flg = 1;
  1232. X    }
  1233. X    else if ( free_ent < maxmaxcode )
  1234. X        free_ent++;
  1235. X    if ( bits != n_bits ) {
  1236. X        fprintf(stderr, "\nChange to %d bits\n", n_bits );
  1237. X        bits = n_bits;
  1238. X        col = 0;
  1239. X    }
  1240. X    fprintf(stderr, "%5d%c", code, (col+=6) >= 74 ? (col = 0, '\n') : ' ' );
  1241. X    }
  1242. X    putc( '\n', stderr );
  1243. X    exit( 0 );
  1244. X}
  1245. X
  1246. Xcode_int sorttab[1<<BITS];    /* sorted pointers into htab */
  1247. X
  1248. Xdump_tab()      /* dump string table */
  1249. X{
  1250. X    register int i, first;
  1251. X    register ent;
  1252. X#define STACK_SIZE    15000
  1253. X    int stack_top = STACK_SIZE;
  1254. X    register c;
  1255. X
  1256. X    if(do_decomp == 0) {        /* compressing */
  1257. X    register int flag = 1;
  1258. X
  1259. X    for(i=0; i<hsize; i++) {        /* build sort pointers */
  1260. X        if((long)htabof(i) >= 0) {
  1261. X            sorttab[codetabof(i)] = i;
  1262. X        }
  1263. X    }
  1264. X    first = block_compress ? FIRST : 256;
  1265. X    for(i = first; i < free_ent; i++) {
  1266. X        fprintf(stderr, "%5d: \"", i);
  1267. X        de_stack[--stack_top] = '\n';
  1268. X        de_stack[--stack_top] = '"';
  1269. X        stack_top = in_stack((htabof(sorttab[i])>>maxbits)&0xff,
  1270. X                     stack_top);
  1271. X        for(ent=htabof(sorttab[i]) & ((1<<maxbits)-1);
  1272. X            ent > 256;
  1273. X            ent=htabof(sorttab[ent]) & ((1<<maxbits)-1)) {
  1274. X            stack_top = in_stack(htabof(sorttab[ent]) >> maxbits,
  1275. X                        stack_top);
  1276. X        }
  1277. X        stack_top = in_stack(ent, stack_top);
  1278. X        fwrite( &de_stack[stack_top], 1, STACK_SIZE-stack_top, stderr);
  1279. X        stack_top = STACK_SIZE;
  1280. X    }
  1281. X   } else if(!debug) {  /* decompressing */
  1282. X
  1283. X       for ( i = 0; i < free_ent; i++ ) {
  1284. X       ent = i;
  1285. X       c = tab_suffixof(ent);
  1286. X       if ( isascii(c) && isprint(c) )
  1287. X           fprintf( stderr, "%5d: %5d/'%c'  \"",
  1288. X               ent, tab_prefixof(ent), c );
  1289. X       else
  1290. X           fprintf( stderr, "%5d: %5d/\\%03o \"",
  1291. X               ent, tab_prefixof(ent), c );
  1292. X       de_stack[--stack_top] = '\n';
  1293. X       de_stack[--stack_top] = '"';
  1294. X       for ( ; ent != NULL;
  1295. X           ent = (ent >= FIRST ? tab_prefixof(ent) : NULL) ) {
  1296. X           stack_top = in_stack(tab_suffixof(ent), stack_top);
  1297. X       }
  1298. X       fwrite( &de_stack[stack_top], 1, STACK_SIZE - stack_top, stderr );
  1299. X       stack_top = STACK_SIZE;
  1300. X       }
  1301. X    }
  1302. X}
  1303. X
  1304. Xint
  1305. Xin_stack(c, stack_top)
  1306. X    register c, stack_top;
  1307. X{
  1308. X    if ( (isascii(c) && isprint(c) && c != '\\') || c == ' ' ) {
  1309. X        de_stack[--stack_top] = c;
  1310. X    } else {
  1311. X        switch( c ) {
  1312. X        case '\n': de_stack[--stack_top] = 'n'; break;
  1313. X        case '\t': de_stack[--stack_top] = 't'; break;
  1314. X        case '\b': de_stack[--stack_top] = 'b'; break;
  1315. X        case '\f': de_stack[--stack_top] = 'f'; break;
  1316. X        case '\r': de_stack[--stack_top] = 'r'; break;
  1317. X        case '\\': de_stack[--stack_top] = '\\'; break;
  1318. X        default:
  1319. X        de_stack[--stack_top] = '0' + c % 8;
  1320. X        de_stack[--stack_top] = '0' + (c / 8) % 8;
  1321. X        de_stack[--stack_top] = '0' + c / 64;
  1322. X        break;
  1323. X        }
  1324. X        de_stack[--stack_top] = '\\';
  1325. X    }
  1326. X    return stack_top;
  1327. X}
  1328. X#endif /* DEBUG */
  1329. X
  1330. Xvoid
  1331. Xwriteerr()
  1332. X{
  1333. X    perror ( ofname );
  1334. X    unlink ( ofname );
  1335. X    exit ( 1 );
  1336. X}
  1337. X
  1338. Xvoid
  1339. Xcopystat(ifname, ofname)
  1340. Xchar *ifname, *ofname;
  1341. X{
  1342. X#ifdef unix
  1343. X    struct stat statbuf;
  1344. X    int mode;
  1345. X    time_t timep[2];
  1346. X
  1347. X    fclose(stdout);
  1348. X    if (stat(ifname, &statbuf)) {               /* Get stat on input file */
  1349. X    perror(ifname);
  1350. X    return;
  1351. X    }
  1352. X    if ((statbuf.st_mode & S_IFMT/*0170000*/) != S_IFREG/*0100000*/) {
  1353. X    if(quiet)
  1354. X        fprintf(stderr, "%s: ", ifname);
  1355. X    fprintf(stderr, " -- not a regular file: unchanged");
  1356. X    exit_stat = 1;
  1357. X    } else if (statbuf.st_nlink > 1) {
  1358. X    if(quiet)
  1359. X        fprintf(stderr, "%s: ", ifname);
  1360. X    fprintf(stderr, " -- has %d other links: unchanged",
  1361. X        statbuf.st_nlink - 1);
  1362. X    exit_stat = 1;
  1363. X    } else if (exit_stat == 2 && (!force)) { /* No compression: remove file.Z */
  1364. X    if(!quiet)
  1365. X        fprintf(stderr, " -- file unchanged");
  1366. X    } else {            /* ***** Successful Compression ***** */
  1367. X    exit_stat = 0;
  1368. X    mode = statbuf.st_mode & 07777;
  1369. X    if (chmod(ofname, mode))                /* Copy modes */
  1370. X        perror(ofname);
  1371. X    chown(ofname, statbuf.st_uid, statbuf.st_gid);  /* Copy ownership */
  1372. X    timep[0] = statbuf.st_atime;
  1373. X    timep[1] = statbuf.st_mtime;
  1374. X    utime(ofname, timep);   /* Update last accessed and modified times */
  1375. X    if (unlink(ifname))     /* Remove input file */
  1376. X        perror(ifname);
  1377. X    if(!quiet)
  1378. X        fprintf(stderr, " -- replaced with %s", ofname);
  1379. X    return;     /* Successful return */
  1380. X    }
  1381. X
  1382. X    /* Unsuccessful return -- one of the tests failed */
  1383. X    if (unlink(ofname))
  1384. X    perror(ofname);
  1385. X#endif
  1386. X}
  1387. X/*
  1388. X * This routine returns 1 if we are running in the foreground and stderr
  1389. X * is a tty.
  1390. X */
  1391. X#ifdef unix
  1392. Xforeground()
  1393. X{
  1394. X    if(bgnd_flag) { /* background? */
  1395. X        return(0);
  1396. X    } else {            /* foreground */
  1397. X        if(isatty(2)) {         /* and stderr is a tty */
  1398. X            return(1);
  1399. X        } else {
  1400. X            return(0);
  1401. X        }
  1402. X    }
  1403. X}
  1404. X#endif
  1405. X
  1406. Xvoid
  1407. Xonintr ( )
  1408. X{
  1409. X    unlink ( ofname );
  1410. X    exit ( 1 );
  1411. X}
  1412. X
  1413. Xvoid
  1414. Xoops ( )        /* wild pointer -- assume bad input */
  1415. X{
  1416. X    if ( do_decomp == 1 )
  1417. X    fprintf ( stderr, "uncompress: corrupt input\n" );
  1418. X    unlink ( ofname );
  1419. X    exit ( 1 );
  1420. X}
  1421. X
  1422. Xvoid
  1423. Xcl_block ()             /* table clear for block compress */
  1424. X{
  1425. X    register long int rat;
  1426. X
  1427. X    checkpoint = in_count + CHECK_GAP;
  1428. X#ifdef DEBUG
  1429. X    if ( debug ) {
  1430. X        fprintf ( stderr, "count: %ld, ratio: ", in_count );
  1431. X        prratio ( stderr, in_count, bytes_out );
  1432. X        fprintf ( stderr, "\n");
  1433. X    }
  1434. X#endif /* DEBUG */
  1435. X
  1436. X    if(in_count > 0x007fffff) { /* shift will overflow */
  1437. X    rat = bytes_out >> 8;
  1438. X    if(rat == 0) {          /* Don't divide by zero */
  1439. X        rat = 0x7fffffff;
  1440. X    } else {
  1441. X        rat = in_count / rat;
  1442. X    }
  1443. X    } else {
  1444. X    rat = (in_count << 8) / bytes_out;      /* 8 fractional bits */
  1445. X    }
  1446. X    if ( rat > ratio ) {
  1447. X    ratio = rat;
  1448. X    } else {
  1449. X    ratio = 0;
  1450. X#ifdef DEBUG
  1451. X    if(verbose)
  1452. X        dump_tab();     /* dump string table */
  1453. X#endif
  1454. X    cl_hash ( (count_int) hsize );
  1455. X    free_ent = FIRST;
  1456. X    clear_flg = 1;
  1457. X    output ( (code_int) CLEAR );
  1458. X#ifdef DEBUG
  1459. X    if(debug)
  1460. X        fprintf ( stderr, "clear\n" );
  1461. X#endif /* DEBUG */
  1462. X    }
  1463. X}
  1464. X
  1465. Xvoid
  1466. Xcl_hash(hsize)          /* reset code table */
  1467. X    register count_int hsize;
  1468. X{
  1469. X#ifndef XENIX_16    /* Normal machine */
  1470. X    register count_int *htab_p = htab+hsize;
  1471. X#else
  1472. X    register j;
  1473. X    register long k = hsize;
  1474. X    register count_int *htab_p;
  1475. X#endif
  1476. X    register long i;
  1477. X    register long m1 = -1;
  1478. X
  1479. X#ifdef XENIX_16
  1480. X    for(j=0; j<=8 && k>=0; j++,k-=8192) {
  1481. X    i = 8192;
  1482. X    if(k < 8192) {
  1483. X        i = k;
  1484. X    }
  1485. X    htab_p = &(htab[j][i]);
  1486. X    i -= 16;
  1487. X    if(i > 0) {
  1488. X#else
  1489. X    i = hsize - 16;
  1490. X#endif
  1491. X    do {                /* might use Sys V memset(3) here */
  1492. X        *(htab_p-16) = m1;
  1493. X        *(htab_p-15) = m1;
  1494. X        *(htab_p-14) = m1;
  1495. X        *(htab_p-13) = m1;
  1496. X        *(htab_p-12) = m1;
  1497. X        *(htab_p-11) = m1;
  1498. X        *(htab_p-10) = m1;
  1499. X        *(htab_p-9) = m1;
  1500. X        *(htab_p-8) = m1;
  1501. X        *(htab_p-7) = m1;
  1502. X        *(htab_p-6) = m1;
  1503. X        *(htab_p-5) = m1;
  1504. X        *(htab_p-4) = m1;
  1505. X        *(htab_p-3) = m1;
  1506. X        *(htab_p-2) = m1;
  1507. X        *(htab_p-1) = m1;
  1508. X        htab_p -= 16;
  1509. X    } while ((i -= 16) >= 0);
  1510. X#ifdef XENIX_16
  1511. X    }
  1512. X    }
  1513. X#endif
  1514. X    for ( i += 16; i > 0; i-- )
  1515. X        *--htab_p = m1;
  1516. X}
  1517. X
  1518. Xvoid
  1519. Xprratio(stream, num, den)
  1520. XFILE *stream;
  1521. Xlong int num, den;
  1522. X{
  1523. X    register int q;         /* Doesn't need to be long */
  1524. X
  1525. X    if(num > 214748L) {             /* 2147483647/10000 */
  1526. X        q = num / (den / 10000L);
  1527. X    } else {
  1528. X        q = 10000L * num / den;     /* Long calculations, though */
  1529. X    }
  1530. X    if (q < 0) {
  1531. X        putc('-', stream);
  1532. X        q = -q;
  1533. X    }
  1534. X    fprintf(stream, "%d.%02d%%", q / 100, q % 100);
  1535. X}
  1536. X
  1537. Xvoid
  1538. Xversion()
  1539. X{
  1540. X    fprintf(stderr, "%s\n", rcs_ident);
  1541. X    fprintf(stderr, "Options: ");
  1542. X#ifdef vax
  1543. X    fprintf(stderr, "vax, ");
  1544. X#endif
  1545. X#ifdef NO_UCHAR
  1546. X    fprintf(stderr, "NO_UCHAR, ");
  1547. X#endif
  1548. X#ifdef SIGNED_COMPARE_SLOW
  1549. X    fprintf(stderr, "SIGNED_COMPARE_SLOW, ");
  1550. X#endif
  1551. X#ifdef XENIX_16
  1552. X    fprintf(stderr, "XENIX_16, ");
  1553. X#endif
  1554. X#ifdef COMPATIBLE
  1555. X    fprintf(stderr, "COMPATIBLE, ");
  1556. X#endif
  1557. X#ifdef DEBUG
  1558. X    fprintf(stderr, "DEBUG, ");
  1559. X#endif
  1560. X#ifdef BSD4_2
  1561. X    fprintf(stderr, "BSD4_2, ");
  1562. X#endif
  1563. X    fprintf(stderr, "BITS = %d\n", BITS);
  1564. X}
  1565. X
  1566. X/*
  1567. X *    Get the size of the file.  Although this is also possible on
  1568. X *    the Amiga, I have not yet implemented this as it is apparently
  1569. X *    used only to adapt the algorithm to make it more efficient for
  1570. X *    small files.  For files on the standard input, the file size
  1571. X *    is unknown, and 100000 is assumed, so we use that value here.
  1572. X *    Fred Fish  14-Jan-86
  1573. X */
  1574. X
  1575. Xlong
  1576. Xgetfilesize (name)
  1577. Xchar *name;
  1578. X{
  1579. X#ifdef unix
  1580. X    struct stat statbuf;
  1581. X
  1582. X    stat ( *fileptr, &statbuf );
  1583. X    return (statbuf.st_size);
  1584. X#else
  1585. X    return (100000);
  1586. X#endif
  1587. X}
  1588. X
  1589. X#ifndef unix
  1590. X#ifndef AMIGA
  1591. Xvoid
  1592. Xperror (arg)
  1593. Xchar *arg;
  1594. X{
  1595. X    if (arg && *arg) {
  1596. X        fprintf (stderr, "%s: ", arg);
  1597. X    }
  1598. X    fprintf (stderr, "<unknown error>\n");
  1599. X}
  1600. X#endif
  1601. X#endif
  1602. END_OF_FILE
  1603. if test 40885 -ne `wc -c <'src/compress/compress.c'`; then
  1604.     echo shar: \"'src/compress/compress.c'\" unpacked with wrong size!
  1605. fi
  1606. # end of 'src/compress/compress.c'
  1607. fi
  1608. echo shar: End of archive 12 \(of 16\).
  1609. cp /dev/null ark12isdone
  1610. MISSING=""
  1611. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ; do
  1612.     if test ! -f ark${I}isdone ; then
  1613.     MISSING="${MISSING} ${I}"
  1614.     fi
  1615. done
  1616. if test "${MISSING}" = "" ; then
  1617.     echo You have unpacked all 16 archives.
  1618.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  1619. else
  1620.     echo You still need to unpack the following archives:
  1621.     echo "        " ${MISSING}
  1622. fi
  1623. ##  End of shell archive.
  1624. exit 0
  1625. -- 
  1626. Submissions to comp.sources.amiga and comp.binaries.amiga should be sent to:
  1627.     amiga@cs.odu.edu    
  1628. or    amiga@xanth.cs.odu.edu    ( obsolescent mailers may need this address )
  1629. or    ...!uunet!xanth!amiga    ( very obsolescent mailers need this address )
  1630.  
  1631. Comments, questions, and suggestions should be addressed to ``amiga-request''
  1632. (please only use ``amiga'' for actual submissions) at the above addresses.
  1633.